import java.util.Arrays;
import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 石方旭
 * Date: 2022-03-22
 * Time: 13:28
 */
public class TestDemo {

    public static boolean isLeapYear(int year) {
        if(year%4==0&&year%100!=0||year%400==0) {
            return true;
        }
        return false;
    }

    public static void main1(String[] args) {
        Scanner scan = new Scanner(System.in);
        System.out.println("请您输入year判断是否是闰年？：>");
        int year =scan.nextInt();
        if(isLeapYear(year)) {
            //是闰年
            System.out.print(year+"是闰年");
        } else {
            System.out.print(year+"不是闰年");
        }
    }

    public static int fac1(int num) {
        int ret =1;
        for (int i = 1; i <= num; i++) {
            ret*=i;
        }
        return ret;
    }
    public static int fac(int n) {
        int sum =0;
        for (int i = 1; i <= n; i++) {
            sum = sum+fac1(i);
        }
        return sum;
    }
    public static void main2(String[] args) {
        //实现1！+2！+3！+4！+5！
        System.out.println(fac(5));
    }

    public static void Swap1(int x,int y) {
        int tmp = x;
        x = y;
        y = tmp;
    }
    public static void main3(String[] args) {
         int a =10;
         int b =20;
         Swap1(a,b);
        System.out.println("交换之后a为："+a);
        System.out.println("交换之后b为："+b);
    }
    public static void Swap(int[] array) {
        int tmp = array[0];
        array[0] = array[1];
        array[1] = tmp;
    }
    public static void main4(String[] args) {
        int[] array =new int[]{10,20};
        Swap(array);
        System.out.println(Arrays.toString(array));
    }
//    public static int add(int a,int b) {
//        return a+b;
//    }
    public static int add(double a,int b) {
        return (int)a+b;
    }
    public static double add(int a,double b) {
        return (double)a+b;
    }
//    public static double add(double a,double b) {
//        return a+b;
//    }
    public static void main5(String[] args) {
        int a =10;
        int b =20;
        int c =20;
        double d =3.14;
        double e =9.89;
        System.out.println(add(a, e));
        System.out.println(add(d, b));
    }
//
//    public static int add(int x, int y){
//        return x + y;
//    }
//    public static double add(double x, double y){
//        return x + y;
//    }
//    public static void main6(String[] args) {
//        add(1,2);
//        add(1.5, 2.5);
//    }

    public static int factor(int n) {
        if(n==1) {
            return 1;
        }
        return n* factor(n-1);
    }

    public static void main8(String[] args) {
        System.out.println(factor(5));
    }

    public static void printEveryNum(int num) {
        if(num<=9) {
            System.out.print(num+" ");
            return;
        }
        printEveryNum(num/10);
        System.out.print(num%10+" ");
    }

    public static int sum(int num) {
        if(num==0) {
            return 0;
        }
        return num+sum(num-1);
    }

    public static void main7(String[] args) {
        int a =10;
        System.out.println(sum(100));
    }

    public static int everyoneSum(int num) {
        if(num<=9) {
            return num;
        }
        return num%10+everyoneSum(num/10);
    }
    public static void main9(String[] args) {
        int num =1234;
        System.out.println(everyoneSum(1234));
    }

    public static int fib(int n) {
        if(n==0||n==1) {
            return n;
        }
     return fib(n-1)+fib(n-2);

    }
    public static void main11(String[] args) {
        System.out.println(fib(5));
        System.out.println(fib(6));
        System.out.println(fib(7));
    }

    /**
     * 在同一个类中定义多个方法：要求不仅可以求两个整数的最大值，
     * 还可以求两个小数的最大值，以及两个小数和一个整数的大小关系
     * @param args
     */
    public static int max(int m,int n) {
        return m>n?m:n;
    }

    public static double max(double m,double n) {
        return m>n?m:n;
    }

    public static void max(int m,double n) {
        if(m>n) {
            System.out.println(m+"与"+n+"最大值是"+m);
        }else {
            System.out.println(m+"与"+n+"最大值是"+n);
        }
    }
    public static void main12(String[] args) {
        int a =10;
        int b =20;
        double c=1.8;
        double d =2.6;
        System.out.println(a+"与"+b+"最大值是"+max(a, b));//两个整形比较大小
        System.out.println(c+"与"+d+"最大值是"+max(c, d));//两个小数比较大小
        max(a, c);//不同类型比较大小
    }

    /**
     * 实现一个方法 avg, 以数组为参数, 求数组中所有元素的平均值(注意方法的返回值类型).
     * @param args
     */

    public static double avg(int[] array) {
        double sum =0;
        for (int i = 0; i < array.length; i++) {
            sum+=array[i];
        }
        double tmp =(sum*1.0)/ array.length;
        return tmp;
    }

    public static void main13(String[] args) {
        int[] array =new int[]{1,2,3,4,5,6,7,8,9,10};
        System.out.println(avg(array));
    }

    /**
     * 实现一个方法 sum, 以数组为参数, 求数组所有元素之和.
     * @param args
     */

    public static int sum1(int[] array) {
        int sum =0;
        for (int x:array) {
            sum+=x;
        }
        return sum;
    }

    public static void main14(String[] args) {
        int[] array =new int[]{1,2,3,4,5,6,7,8,9,10};
        System.out.println(sum1(array));
    }

    /**
     * 实现一个方法 transform, 以数组为参数, 循环将数组中的每个元素 乘以 2 ,
     * 并设置到对应的数组元素上. 例如 原数组为 {1, 2, 3}, 修改之后为 {2, 4, 6}
     * @param args
     */

    public static void transform(int[] array) {
        for (int i = 0; i < array.length; i++) {
            array[i]*=2;
        }
    }
    public static void main15(String[] args) {
        int[] array =new int[]{1,2,3};
        transform(array);
        System.out.println(Arrays.toString(array));
    }

    /**
     * 实现一个方法 printArray, 以数组为参数, 循环访问数组中的每个元素, 打印每个元素的值.
     * @param args
     */

    public static void printArray(int[] array) {
        for (int i = 0; i < array.length; i++) {
            System.out.print(array[i]+" ");
        }
    }
    public static void main16(String[] args) {
        int[] array =new int[]{1,2,3,4,5,6,7,8,9,10};
        printArray(array);
    }

    /**
     * 创建一个 int 类型的数组, 元素个数为 100, 并把每个元素依次设置为 1 - 100
     * @param args
     */
    public static void main18(String[] args) {
        int[] array =new int[100];
        for (int i = 0; i < array.length; i++) {
            System.out.print((i+1)+" ");
        }
    }

    /**
     * 实现一个方法 toString, 把一个整型数组转换成字符串.
     * 例如数组 {1, 2, 3} , 返回的字符串为 "[1, 2, 3]", 注意 逗号 的位置和数量.
     * @param args
     */
    public static String toString1(int[] array) {
        String str ="[";
        for (int i = 0; i < array.length; i++) {
            str+=array[i];
            if(i!=array.length-1) {
                str+=",";
            }
        }
        str+="]";
        return str;
    }
    public static void main17(String[] args) {
        int[] array ={1,2,3,4,5,6,7,8,9,4,1,0};
        System.out.println(toString1(array));
    }

    /**
     * 实现一个方法 copyOf, 对一个整型数组进行拷贝, 得到一个新的数组.
     * @param args
     */
    public static int[] copyOf1(int[] array) {
        int[] tmp =new int[array.length];
        for (int i = 0; i < array.length; i++) {
            tmp[i]=array[i];
        }
        return tmp;
    }
    public static void main19(String[] args) {
        int[] array ={1,2,3,4,5,6,7,8,9,4,1,0};
        int[] tmp =copyOf1(array);
        for (int x: tmp) {
            System.out.print(x+" ");
        }
    }

    public static void bubbleSort(int[] array) {
        for (int i = 0; i < array.length-1; i++) {
            for (int j = 0; j < array.length-1-i; j++) {
                if(array[j]>array[j+1]) {
                    int tmp =array[j];
                    array[j] =array[j+1];
                    array[j+1] =tmp;
                }
            }
        }
    }
    public static void main21(String[] args) {
        int[] array =new int[]{78,56,93,52,1,4,20,30,56,2,5,9,7};
        bubbleSort(array);
        System.out.println(Arrays.toString(array));
    }

    public static void main(String[] args) {
       // int[][] array =new int[][]{{78,56,93,52},{1,4,20,30,56},{2,5,9,7}};
        int[] array =new int[]{78,56,93,52,1,4,20,30,56,2,5,9,7};
        System.out.println(Arrays.deepToString(array));
    }

}
